<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Linear Style 番茄时钟</title>
    <style>
        :root {
            --primary: #5E6AD2;
            --success: #22C55E;
            --background: #ffffff;
            --text: #2A2A2A;
        }

        body {
            font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, sans-serif;
            background: #f9f9fb;
            display: flex;
            justify-content: center;
            align-items: center;
            min-height: 100vh;
            margin: 0;
            padding: 20px;
        }

        .container {
            background: var(--background);
            padding: 32px;
            border-radius: 16px;
            box-shadow: 0 8px 32px rgba(0,0,0,0.05);
            width: 100%;
            max-width: 400px;
        }

        h1 {
            color: var(--text);
            margin-bottom: 32px;
            font-size: 24px;
            text-align: center;
        }

        .input-group {
            margin-bottom: 24px;
        }

        label {
            display: block;
            margin-bottom: 8px;
            color: #666;
            font-size: 14px;
        }

        input {
            width: 100%;
            padding: 12px;
            border: 1px solid #e5e5e5;
            border-radius: 8px;
            font-size: 16px;
            box-sizing: border-box;
        }

        .timer {
            font-size: 48px;
            font-weight: 600;
            text-align: center;
            margin: 32px 0;
            color: var(--text);
        }

        .buttons {
            display: flex;
            gap: 12px;
            margin-top: 24px;
        }

        button {
            flex: 1;
            padding: 14px;
            border: none;
            border-radius: 8px;
            font-weight: 500;
            cursor: pointer;
            transition: all 0.2s;
        }

        .start-btn {
            background: var(--primary);
            color: white;
        }

        .reset-btn {
            background: #f4f4f5;
            color: var(--text);
        }

        button:hover {
            opacity: 0.9;
        }

        .work .timer { color: var(--primary); }
        .break .timer { color: var(--success); }
    </style>
</head>
<body>
    <div class="container">
        <h1>番茄工作时钟</h1>
        
        <div class="input-group">
            <label>工作时间（分钟）</label>
            <input type="number" id="workTime" min="1" value="25">
        </div>
        
        <div class="input-group">
            <label>休息时间（分钟）</label>
            <input type="number" id="breakTime" min="1" value="5">
        </div>

        <div class="timer" id="display">25:00</div>

        <div class="buttons">
            <button class="start-btn" id="startBtn">开始</button>
            <button class="reset-btn" id="resetBtn">重置</button>
        </div>
    </div>

    <script>
        let timer = null;
        let isRunning = false;
        let isWork = true;
        let remainingTime = 0;

        // 从localStorage加载设置
        const workTimeInput = document.getElementById('workTime');
        const breakTimeInput = document.getElementById('breakTime');
        const display = document.getElementById('display');
        const startBtn = document.getElementById('startBtn');
        const resetBtn = document.getElementById('resetBtn');
        const container = document.querySelector('.container');

        // 加载保存的设置
        const savedSettings = JSON.parse(localStorage.getItem('pomodoroSettings')) || {};
        workTimeInput.value = savedSettings.workTime || 25;
        breakTimeInput.value = savedSettings.breakTime || 5;

        // 加载状态
        const savedState = JSON.parse(localStorage.getItem('pomodoroState'));
        if (savedState) {
            isRunning = savedState.isRunning;
            isWork = savedState.isWork;
            remainingTime = savedState.remainingTime;
            
            if (isRunning) {
                remainingTime = Math.max(0, savedState.remainingTime - 
                    Math.floor((Date.now() - savedState.timestamp)/1000));
                updateDisplay(remainingTime);
                startTimer();
            } else {
                updateDisplay(isWork ? workTimeInput.value * 60 : breakTimeInput.value * 60);
            }
        }

        function updateDisplay(seconds) {
            const mins = Math.floor(seconds / 60).toString().padStart(2, '0');
            const secs = (seconds % 60).toString().padStart(2, '0');
            display.textContent = `${mins}:${secs}`;
        }

        function startTimer() {
            if (!isRunning) {
                const workTime = workTimeInput.value * 60;
                const breakTime = breakTimeInput.value * 60;
                remainingTime = remainingTime || (isWork ? workTime : breakTime);

                timer = setInterval(() => {
                    remainingTime--;
                    updateDisplay(remainingTime);

                    if (remainingTime <= 0) {
                        clearInterval(timer);
                        isWork = !isWork;
                        remainingTime = isWork ? workTime : breakTime;
                        container.classList.toggle('work', isWork);
                        container.classList.toggle('break', !isWork);
                        if (!isWork) new Audio('https://actions.google.com/sounds/v1/alarms/beep_short.ogg').play();
                        startTimer();
                    }
                }, 1000);

                isRunning = true;
                startBtn.textContent = '暂停';
            } else {
                clearInterval(timer);
                isRunning = false;
                startBtn.textContent = '继续';
            }

            saveState();
        }

        function saveState() {
            localStorage.setItem('pomodoroSettings', JSON.stringify({
                workTime: workTimeInput.value,
                breakTime: breakTimeInput.value
            }));

            localStorage.setItem('pomodoroState', JSON.stringify({
                isRunning,
                isWork,
                remainingTime,
                timestamp: Date.now()
            }));
        }

        startBtn.addEventListener('click', startTimer);

        resetBtn.addEventListener('click', () => {
            clearInterval(timer);
            isRunning = false;
            isWork = true;
            remainingTime = workTimeInput.value * 60;
            updateDisplay(remainingTime);
            startBtn.textContent = '开始';
            container.classList.remove('break');
            container.classList.add('work');
            localStorage.removeItem('pomodoroState');
        });

        // 初始化状态
        container.classList.add(isWork ? 'work' : 'break');
        updateDisplay(isWork ? workTimeInput.value * 60 : breakTimeInput.value * 60);
    </script>
</body>
</html>